<?php

	require_once("../../../includes/config.inc.php");
	$ext = array("JPG","jpg");
	
	if($_GET['action'] == "insert"){
	
		mysql_query("INSERT INTO slides (link) VALUES ('".$_POST['url']."')") or die(mysql_error());
		$last_id = mysql_fetch_row(mysql_query("SELECT LAST_INSERT_ID()"));
		$arquivo = $last_id[0];
		
		$file = 'image1';
		if(!empty($_FILES[$file])){
			$dir = "../../../images/slides";
			$name = $_FILES[$file]['name'];
			$tmp = $_FILES[$file]['tmp_name'];
			$count = in_array(substr($name, -3), $ext);
			$renomeia = $arquivo.".".substr($name, -3);
			move_uploaded_file($tmp, $dir."/".$renomeia);
		}
			
		header("location: ../../index.php?option=into&view=slides");
	
	}else if($_GET['action'] == "update"){
		
		mysql_query("UPDATE slides SET link = '".$_POST['url']."' WHERE idslides = ".$_GET['itemId']);
		$arquivo = $_GET['itemId'];
		
		$file = 'image1';
		if(!empty($_FILES[$file])){
			$dir = "../../../images/slides";
			$name = $_FILES[$file]['name'];
			$tmp = $_FILES[$file]['tmp_name'];
			$count = in_array(substr($name, -3), $ext);
			$renomeia = $arquivo.".".substr($name, -3);
			move_uploaded_file($tmp, $dir."/".$renomeia);
		}
		header("location: ../../index.php?option=into&view=slides");
	
	}else if($_GET['action'] == "delete"){
		
		$excluir = $_POST['excluir'];
		for($i=0; $i<count($excluir); $i++){
			
			$arquivo = $excluir[$i];	
			$dir = "../../../images/slides";
			$renomeia = $arquivo.".jpg";
			if(file_exists($dir."/".$renomeia)){
				unlink($dir."/".$renomeia);
			}
			mysql_query("DELETE FROM slides WHERE idslides = ".$excluir[$i]) or die (mysql_error());
		
		}
		header("location: ../../index.php?option=into&view=slides");
		
	}else if($_GET['action'] == "config"){
		
		$atualizar = $_POST['excluir'];
		if(!empty($atualizar)){
			for($i=0; $i<1; $i++){
				
				header("location: ../../index.php?option=into&view=slides&action=update&itemId=".$atualizar[$i]);
			
			}
		}else{
			header("location: ../../index.php?option=into&view=slides");	
		}
	
	}else{
		
		echo "<h1>Houve um erro inesperado!</h1><p>Entre em contato com Murilo Machado<br />+55 51 820933.04<br />murilomsilva@terra.com.br</p>";
	
	}
	
	

?>